//
//  RNCalendarView.m
//  RNCalendarView
//
//  Created by DJ on 2019/4/16.
//  Copyright © 2019 DJ. All rights reserved.
//

#import "RNCalendarView.h"
#import "CalendarView.h"
#import <React/RCTUIManager.h>

@interface RNCalendarView()<DateSelectedDelegate>

@end

@implementation RNCalendarView
    
RCT_EXPORT_MODULE();
//选中日期传给RN
RCT_EXPORT_VIEW_PROPERTY(onClickDate, RCTBubblingEventBlock);
//改变高度传给RN
RCT_EXPORT_VIEW_PROPERTY(onChangeCalendarHeight, RCTBubblingEventBlock);
//需要选中的日期数组
RCT_EXPORT_VIEW_PROPERTY(dateArray, NSArray);
//日历长
RCT_EXPORT_VIEW_PROPERTY(width, CGFloat);
//日历高
RCT_EXPORT_VIEW_PROPERTY(height, CGFloat);
//当天的颜色
RCT_EXPORT_VIEW_PROPERTY(todayColor, NSString);
//选中的颜色
RCT_EXPORT_VIEW_PROPERTY(selectionColor, NSString);
//含有事件的日期默认颜色
RCT_EXPORT_VIEW_PROPERTY(eventDefaultColor, NSString);
//星期几的颜色
RCT_EXPORT_VIEW_PROPERTY(weekdayTextColor, NSString);
//头部标题颜色
RCT_EXPORT_VIEW_PROPERTY(headerTitleColor, NSString);
//日历背景颜色
RCT_EXPORT_VIEW_PROPERTY(calendarBackgroundColor, NSString);
//边框颜色
RCT_EXPORT_VIEW_PROPERTY(borderDefaultColor, NSString);
//标题默认颜色
RCT_EXPORT_VIEW_PROPERTY(titleDefaultColor, NSString);
//事件图片
RCT_EXPORT_VIEW_PROPERTY(imageString, NSString);
//是否显示阴历
RCT_EXPORT_VIEW_PROPERTY(showLunar, BOOL);
//是否是中文
RCT_EXPORT_VIEW_PROPERTY(chinese, BOOL);
//是否显示事件
RCT_EXPORT_VIEW_PROPERTY(showEvent, BOOL);
//是否多选
RCT_EXPORT_VIEW_PROPERTY(allowsMultipleSelection, BOOL);
//是否显示正方形
RCT_EXPORT_VIEW_PROPERTY(square, BOOL);
//最小和最大日期
RCT_EXPORT_VIEW_PROPERTY(minAndMaxDate, NSArray);

#pragma mark RN上显示的View
-(UIView *)view{
    CalendarView *calendarView = [[CalendarView alloc] init];
    calendarView.delegate = self;
    return calendarView;
}

#pragma mark 代理方法
- (void)dateSelect:(NSMutableDictionary *)dateDic calendarView:(CalendarView *)calendarView{
    if(calendarView.onClickDate){
        calendarView.onClickDate(dateDic);
    }
}

-(void)changeCalendarHeight:(NSDictionary *)dateDic calendarView:(CalendarView *)calendarView{
    if(calendarView.onChangeCalendarHeight){
        calendarView.onChangeCalendarHeight(dateDic);
    }
}

#pragma mark 上个月
RCT_EXPORT_METHOD(lastMonth:(nonnull NSNumber *)reactTag){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view jumpToLastMonth];
        }
    }];
}

#pragma mark 下个月
RCT_EXPORT_METHOD(nextMonth:(nonnull NSNumber *)reactTag){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view jumpToNextMonth];
        }
    }];
}

#pragma mark 回到今天
RCT_EXPORT_METHOD(toToday:(nonnull NSNumber *)reactTag){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view backToToday];
        }
    }];
}

#pragma mark 跳转到指定日期
RCT_EXPORT_METHOD(specifiedDate:(nonnull NSNumber *)reactTag data:data){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view jumpToSpecifiedDate:data];
        }
    }];
}

#pragma mark 周显示
RCT_EXPORT_METHOD(week:(nonnull NSNumber *)reactTag){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view showWeek];
        }
    }];
}

#pragma mark 月显示
RCT_EXPORT_METHOD(month:(nonnull NSNumber *)reactTag){
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, CalendarView *> *viewRegistry) {
        CalendarView *view = viewRegistry[reactTag];
        if (![view isKindOfClass:[CalendarView class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting TBNAnimationView, got: %@", view);
        } else {
            [view showMonth];
        }
    }];
}
    

@end
